/**
 * 创元汇 - CheckVersion
 * http://menger.me
 * @大梦
 */

'use strict';

import React from 'react'
import {Linking, StyleSheet, Text, View,} from 'react-native'

import Theme from '../components/common/Theme'
import Constants from '../constants/Constants'
import StatusCode from '../constants/StatusCode'
import ServicesApi from '../constants/ServicesApi'
import GlobalStyles from '../constants/GlobalStyles'
import AlertManager from '../components/common/AlertManager'
import { toastShort, toastLong } from './utilsToast'

let VERSION_INFO = {
    version_code: "2.1.0",
    title: "更新内容",
    content: [
        { id: 1, value: "修复已知问题" },
        { id: 2, value: "优化系统性能" }
    ],
    link_android: "https://fir.im/cyhapp",
    link_ios: "https://apps.apple.com/cn/app/id1369704733"
}

/**
 * 提交当前版本
 * Author: 梦
 * Date: 2019/01/10
 * Return: 版本信息
 */
const checkVersion = () => {
    let url = ServicesApi.CHECK_UPDATE;
    let data = {
        platform: __IOS__,
        version_code: Constants.VERSION_CODE,
        version_name: Constants.VERSION_NAME,
    };

    Services.post(url, data, true)
        .then((result) => {
            console.log('checkVersion result---->', result);
            global.IS_ONLINE = result.data.is_online;
            if (result.code === StatusCode.SUCCESS_CODE && result.data.new_version === 1) {
                showVersionPopupView(result.data.version);
            }
        })
        .catch((error) => {
            console.log('error---->', error);
        })
};

/**
 * 新版本弹窗
 * Author: 梦
 * Date: 2019/01/10
 */
const showVersionPopupView = (data) => {
    let {title, version_code, content, link_ios, link_android} = data;
    let link = __IOS__ ? link_ios : link_android;
    let detail = Array.isArray(content) ? content.map((item, index) => {
        return (
            <Text key={item.id} style={styles.alertItemStyle}>{index + 1}、{item.value}</Text>
        );
    }) : <Text style={styles.alertItemStyle}>{content}</Text>;
    let detailContent = (
        <View style={styles.alertContent}>
            <View style={styles.versionContent}>
                <Text style={styles.versionStyle}>{version_code}</Text>
            </View>
            <View style={styles.detailContentStyle}>
                <Text style={styles.detailContentTitle}>{title}</Text>
                {detail}
            </View>
        </View>
    );
    const params = {
        title: '发现新版本',
        style: {width: GlobalStyles.screenWidth - 50,},
        detail: detailContent,
        actions: [
            {
                title: '立即升级',
                onPress: () => submitUpdate(link),
            }
        ],
        actionContainerStyle: {width: GlobalStyles.screenWidth - 50},
    };
    AlertManager.show(params, {
        modal: true,
    });
};

/**
 * 跳转下载页面
 * Author: 梦
 * Date: 2019/01/10
 */
const submitUpdate = (url) => {
    Linking.canOpenURL(url)
        .then(supported => {
            if (!supported) {
                toastShort('无法打开该链接');
                return;
            } else {
                return (
                    Linking.openURL(url)
                        .catch(e => {
                            console.log('e---->', e);
                            toastShort('打开链接出错，请稍后重试');
                        })
                );
            }
        })
        .catch(error => {
            console.log('error---->', error);
            toastShort('打开链接出错，请稍后重试');
        });
};

const styles = StyleSheet.create({
    alertContent: {
        paddingTop: 10,
        paddingBottom: 10,
        justifyContent: 'flex-start',
        width: Theme.screenWidth - 100,
        maxWidth: Theme.screenWidth - 100,
    },
    versionContent: {
        marginBottom: 15,
        paddingBottom: 15,
        borderColor: '#333',
        alignItems: 'center',
        borderBottomWidth: Theme.minPixel,
    },
    versionStyle: {
        color: '#fff',
        borderRadius: 12,
        paddingVertical: 3,
        overflow: 'hidden',
        paddingHorizontal: 15,
        backgroundColor: '#b08b59',
    },
    detailContentStyle: {
        minHeight: 80,
    },
    detailContentTitle: {
        fontSize: 14,
        color: '#333',
        lineHeight: 20,
        marginBottom: 5,
        fontWeight: '600',
        fontFamily: 'PingFangSC-Regular'
    },
    alertItemStyle: {
        color: '#333',
        lineHeight: 20,
        fontFamily: 'PingFangSC-Regular',
    },
});

export default checkVersion;